<?php

class Service_DiscogsDigital extends Service implements IService
{
    
    static $moduleName = 'Discogs Digital Parser';
    static $author = 'KnKill';
    static $version = '0.25';
    static $site = 'www.discogs.com/digital';
    static $mask = '.*discogs.com\/(digital|mp3).*';
    static $metaFields = array(META_LABEL, META_GENRES, META_RELEASE_DATE, META_CATALOG);
    
    private $content;

    public function __construct($url) {
        $this->content = getPage($url);
        phpQuery::newDocumentHTML($this->content);
    }
    
    public function getReleaseArtists() {
        $title = pq('.bundleTitle')->eq(0)->text();
        preg_match('/(.*)–(.*)/msu', $title, $title);
        $artists = array_filter(explode(', ', trim($title[1])), function($value) {
            return !empty($value);
        });
        return array_unique($artists);
    }
    
    public function getReleaseTitle() {
        $title = pq('.bundleTitle')->eq(0)->text();
        preg_match('/(.*)–(.*)/msu', $title, $title);
        return trim($title[2]);
    }
    
    public function getGenres() {
        $genre = pq('.bundleDetails > .no_style')->eq(0)->find('li')->eq(0)->find('a')->text();
        $genre = trim($genre);
        $genre = str_replace('Drum And Bass', 'Drum & Bass', $genre);
        return array($genre);
    }
    
    public function getReleaseDate() {
        $rd = pq('.bundleDetails > .no_style')->eq(0)->find('li')->eq(1)->text();
        preg_match('/Released: (.*)/', $rd, $rd);
        return date('Y-m-00', strtotime(trim($rd[1])));
    }
    
    public function getLabel() {
        $label = pq('.bundleDetails > .no_style')->eq(0)->find('li')->eq(2)->text();
        preg_match('/Cat#:(.*)–(.*)/msu', $label, $label);
        return trim($label[1]);
    }
    
    public function getCatalog() {
        $catalog = pq('.bundleDetails > .no_style')->eq(0)->find('li')->eq(2)->text();
        preg_match('/Cat#:(.*)–(.*)/msu', $catalog, $catalog);
        return trim(str_replace(" ", "", $catalog[2]));
    }
    
    public function getTracklist() {
        $releaseArtists = $this->getReleaseArtists();
        $tracklist = array();
        foreach(pq('.bundleIndex tr') as $track) {
            $pqTrack = pq($track);
            $pqTd = $pqTrack->find('td');
        
            $index = $pqTd->eq(0)->text();
            $trackTitle = $pqTd->eq(1)->text();
            if (preg_match('/(.*)–(.*)/msu', $trackTitle, $matches) == true) {
                $artists = array(trim($matches[1]));
                $trackTitle = trim($matches[2]);
            } else {
                $artists = $releaseArtists;
            }
            
            $length = $pqTrack->find('td.quiet')->eq(1)->text();
            $tracklist[(int)$index] = array(
                'artists' => $artists,
                'title'   => $trackTitle,
                'length'  => $length
            );
            
        }
        return $tracklist;
    }
  
}